package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletAddCardContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.TempCard;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletAddCardActivity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletAddCardPresenter implements WalletAddCardContract.Presenter {

    private WalletAddCardActivity context;
    private final WalletAddCardContract.View view;
    private Gson gson;

    public WalletAddCardPresenter(WalletAddCardActivity view,
                                  WalletAddCardActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void getCardCode() {
        String appToken = InfoProvider.getAppToken(App.getInstance());
        String service = "finance.consumerTempPrepayCardCardNoGet";
        String jsonParams = new JSONObject().toString();

        WalletApiDataSource.getInstance()
                .getTempCardCode(appToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<TempCard>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<TempCard> response) {
                        view.getCardCodeBack(true, response.data.getTemp_card_no());
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.getCardCodeBack(false, msg);
                    }
                });
    }

    @Override
    public void addTempCard(String input, String code) {
        String appToken = InfoProvider.getAppToken(App.getInstance());
        String service = "finance.consumerTempPrepayCardCreate";

        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("card_no", code);
        hashMap.put("mobile", InfoProvider.getUserInfo(App.getInstance()).getMobile());
        hashMap.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        hashMap.put("remark", input);
        String jsonParams = gson.toJson(hashMap);

        WalletApiDataSource.getInstance()
                .addTempCard(appToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse>(context, true) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        view.addTempCardBack(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.addTempCardBack(false, msg);
                    }
                });
    }

    @Override
    public void addNewCard(String name, String code) {
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        String userToken = token.getUser_token();
        String service = "finance.consumerPrepayCardEdit";
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("member_id", token.getMember_id());
        hashMap.put("card_no", code);
        hashMap.put("remark", name);
        String jsonParams = gson.toJson(hashMap);

        WalletApiDataSource.getInstance()
                .addCard(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(context, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.addCardBack(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.addCardBack(false, msg);
                    }
                });
    }

}
